/*
 
 TopMind jLynx "jLynx JDBC Framework"
 Copyright (c) 2004-2007. TopMind Systems Inc.
 All rights reserved.
 
 This file is part of TopMind jLynx.
 
 TopMind jLynx is free software; you can redistribute it and/or modify
 it under the terms of the License. See website for License.
 
 */
package net.sf.jlynx;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Class used to manage database connections and local transactions
 * 
 * $Id: TransactionManager.java 99 2007-06-21 02:51:13Z gritc11 $
 */
final class TransactionManager {

	static final int MYSQL = 1;

	static final int MSSQL = 2;

	static final int ORACLE = 3;

	static final int DB2 = 4;

	static final int HSQL = 5;

	static final int POSTGRESQL = 6;

	static int dbVendor;

	protected static boolean isLookup;

	static Logger logger = LoggerFactory.getLogger(TransactionManager.class);

	/**
	 * Commits local database transaction identified by txID parameter and
	 * closes connection
	 * 
	 * @param txID
	 * @throws SQLException
	 */
	synchronized static void commit(Connection cn) throws SQLException {
		cn.commit();
		cn.close();
		cn = null;
	}

	/**
	 * Rollback local database transaction identified by txID parameter and
	 * closes connection
	 * 
	 * @param txID
	 * @throws SQLException
	 */
	synchronized static void rollback(Connection cn) throws SQLException {
		cn.rollback();
		cn.close();
		cn = null;
	}

	ConnectionBean cb = (ConnectionBean) ConfigParser.getConnectionDefs().get(
			ConfigParser.DEFAULT);

	/**
	 * Open and return a new database connection using jLynx default
	 * configuration
	 * 
	 * @throws java.sql.SQLException
	 * @return java.sql.Connection
	 */
	Connection open() throws SQLException, NamingException {

		if (cb == null) {

			throw new SQLException("Connection named <<" + ConfigParser.DEFAULT
					+ ">> does not exist, i.e. <connection name='default'>");

		}
		String dbUrl = cb.getUrl();
		String dbUser = cb.getUsername();
		String dbPwd = cb.getPassword();
		String jdbcDriver = cb.getDriver();
		String jndiDs = cb.getJndiDataSource();

		logger.debug("opening connection: " + dbUrl);

		Connection conn1 = null;

		if (jndiDs != null) {

			InitialContext ic = new InitialContext();
			DataSource ds = (DataSource) ic.lookup("java:comp/env/" + jndiDs);
			conn1 = ds.getConnection();

		} else {

			try {
				Class.forName(jdbcDriver).newInstance();
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			conn1 = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
			conn1.setReadOnly(false);
		}

		return conn1;
	}

	Connection open(String namedConnection) throws SQLException,
			NamingException {

		cb = (ConnectionBean) ConfigParser.getConnectionDefs().get(
				namedConnection);
		return open();

	}

}